﻿Imports DAO
Public Class TuVanBUS
    Public Shared Function ChonHeThong(dt As DataTable, SolarFuncton As Integer, TongCongSuatPin As Integer, TongCongSuatTai As Integer, DienApTai As Integer, CoThietBiDeBa As Boolean, SoNgaySac As Integer, SoNgaySuDung As Integer) As DataTable
        Dim BangPin = TuVanDAO.ChonPin1(TongCongSuatPin)
        Dim dtp As DataTable = dt
        Dim index As Integer()
        ReDim index(6)
        index(0) = 0
        index(5) = 0
        index(1) = 0
        index(2) = 0
        index(3) = 0
        index(4) = 0
        Dim sotampin0 As Integer = 0
        For i = 1 To BangPin.Rows.Count - 1
            If BangPin.Rows(i).Item("MaLoai").ToString() = "1" Then
                If BangPin.Rows(i).Item("SoDu").ToString() = "0" Then
                    If index(0) = 0 Then
                        index(0) = i
                        sotampin0 = Integer.Parse(BangPin.Rows(i).Item("SoTamPin").ToString())
                    Else
                        If Integer.Parse(BangPin.Rows(i).Item("SoTamPin").ToString()) < sotampin0 Then
                            index(0) = i
                            sotampin0 = Integer.Parse(BangPin.Rows(i).Item("SoTamPin").ToString())
                        End If
                    End If
                Else
                    If index(1) = 0 Then
                        index(1) = i
                    Else
                        Dim sodu1 As Integer = Integer.Parse(BangPin.Rows(index(1)).Item("CongSuat").ToString()) - Integer.Parse(BangPin.Rows(index(1)).Item("SoDu").ToString())
                        Dim sodu2 As Integer = Integer.Parse(BangPin.Rows(i).Item("CongSuat").ToString()) - Integer.Parse(BangPin.Rows(i).Item("SoDu").ToString())
                        If sodu2 <= sodu1 Then
                            If Integer.Parse(BangPin.Rows(i).Item("SoTamPin").ToString()) < Integer.Parse(BangPin.Rows(index(1)).Item("SoTamPin").ToString()) Then
                                index(2) = index(1)
                                index(1) = i
                            Else
                                If index(2) = 0 Then
                                    index(2) = i
                                End If
                            End If
                        Else
                            If index(2) = 0 Then
                                index(2) = i

                            Else
                                Dim sodu3 As Integer = Integer.Parse(BangPin.Rows(index(2)).Item("CongSuat").ToString()) - Integer.Parse(BangPin.Rows(index(2)).Item("SoDu").ToString())
                                If sodu1 <= sodu3 Then
                                    If Integer.Parse(BangPin.Rows(i).Item("SoTamPin").ToString()) < Integer.Parse(BangPin.Rows(index(2)).Item("SoTamPin").ToString()) Then
                                        index(2) = i
                                    End If
                                End If
                            End If
                        End If
                    End If

                End If
            ElseIf BangPin.Rows(i).Item("MaLoai").ToString() = "2" Then
                If BangPin.Rows(i).Item("SoDu").ToString() = "0" Then
                    If index(5) = 0 Then
                        index(5) = i
                        sotampin0 = Integer.Parse(BangPin.Rows(i).Item("SoTamPin").ToString())
                    Else
                        If Integer.Parse(BangPin.Rows(i).Item("SoTamPin").ToString()) < sotampin0 Then
                            index(5) = i
                            sotampin0 = Integer.Parse(BangPin.Rows(i).Item("SoTamPin").ToString())
                        End If
                    End If
                Else
                    If index(3) = 0 Then
                        index(3) = i
                    Else
                        Dim sodu1 As Integer = Integer.Parse(BangPin.Rows(index(3)).Item("CongSuat").ToString()) - Integer.Parse(BangPin.Rows(index(3)).Item("SoDu").ToString())
                        Dim sodu2 As Integer = Integer.Parse(BangPin.Rows(i).Item("CongSuat").ToString()) - Integer.Parse(BangPin.Rows(i).Item("SoDu").ToString())
                        If sodu2 <= sodu1 Then
                            If Integer.Parse(BangPin.Rows(i).Item("SoTamPin").ToString()) < Integer.Parse(BangPin.Rows(index(1)).Item("SoTamPin").ToString()) Then
                                index(4) = index(3)
                                index(3) = i
                            Else
                                If index(4) = 0 Then
                                    index(4) = i
                                End If
                            End If
                        Else
                            If index(4) = 0 Then
                                index(4) = i
                            Else
                                Dim sodu3 As Integer = Integer.Parse(BangPin.Rows(index(2)).Item("CongSuat").ToString()) - Integer.Parse(BangPin.Rows(index(4)).Item("SoDu").ToString())
                                If sodu1 <= sodu3 Then
                                    If Integer.Parse(BangPin.Rows(i).Item("SoTamPin").ToString()) < Integer.Parse(BangPin.Rows(index(4)).Item("SoTamPin").ToString()) Then
                                        index(4) = i
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If

        Next

        For i = 0 To 5


            If index(i) > 0 Then
                Dim row As DataRow = dtp.NewRow()

                row("TenPin") = BangPin.Rows(index(i)).Item("TenPin").ToString()
                row("CongSuatPin") = BangPin.Rows(index(i)).Item("CongSuat").ToString()

                Dim BangMach As New DataTable()
                Dim BangBKD As New DataTable()
                Dim BangAcquy As New DataTable()
                Dim bp As Integer
                If Integer.Parse(BangPin.Rows(index(i)).Item("SoDu").ToString()) > 0 Then
                    bp = Integer.Parse(BangPin.Rows(index(i)).Item("SoTamPin").ToString()) + 1
                Else
                    bp = Integer.Parse(BangPin.Rows(index(i)).Item("SoTamPin").ToString())
                End If
                row("SoLuongPin") = bp.ToString()
                row("GiaPin") = bp * Integer.Parse(row("CongSuatPin").ToString()) * Integer.Parse(BangPin.Rows(index(i)).Item("DonGia").ToString())

                ' Chọn Mạch Điều Khiển
                BangMach = TuVanDAO.ChonMachDieuKhien(bp, Double.Parse(BangPin.Rows(index(i)).Item("Isc").ToString()))
                If BangMach.Rows.Count > 0 Then
                    row("TenMachDieuKhien") = BangMach.Rows(0).Item("TenMach").ToString()
                    row("DongDienMach") = BangMach.Rows(0).Item("DongDien")
                    row("GiaMachDieuKhien") = BangMach.Rows(0).Item("Gia")
                Else
                    row("TenMachDieuKhien") = "0"
                    row("DongDienMach") = 0
                    row("GiaMachDieuKhien") = 0
                End If


                If SolarFuncton = 1 Then
                    BangAcquy = TuVanDAO.ChonAcquy(TongCongSuatPin, SoNgaySac, SoNgaySuDung)
                    Dim indexaq As Integer = 0
                    Dim costaq As Int64 = 0
                    Dim soluongbinh As Integer = 0
                    For y = 1 To BangAcquy.Rows.Count - 1
                        Dim a As Integer
                        If BangAcquy.Rows(y).Item("SoDu").ToString() <> 0 Then
                            a = Integer.Parse(BangAcquy.Rows(y).Item("SoBinh").ToString()) + 1
                        Else
                            a = Integer.Parse(BangAcquy.Rows(y).Item("SoBinh").ToString())
                        End If
                        If costaq = 0 Then
                            indexaq = y
                            costaq = a * Integer.Parse(BangAcquy.Rows(y).Item("Gia").ToString())
                            soluongbinh = a
                        Else
                            Dim temp As Integer = a * Integer.Parse(BangAcquy.Rows(y).Item("Gia").ToString())
                            If costaq > temp Then
                                costaq = temp
                                indexaq = y
                                soluongbinh = a
                            End If
                        End If
                    Next
                    row("TenAcQuy") = BangAcquy.Rows(indexaq).Item("TenAcQuy").ToString()
                    row("DungLuong") = BangAcquy.Rows(indexaq).Item("DongDien")
                    row("SoLuongAcquy") = soluongbinh
                    row("GiaAcquy") = costaq
                Else
                    row("TenAcQuy") = "0"
                    row("DungLuong") = 0
                    row("SoLuongAcquy") = 0
                    row("GiaAcquy") = 0
                End If
                BangBKD = TuVanDAO.ChonBoKichDien(CoThietBiDeBa, TongCongSuatTai, DienApTai)
                If BangBKD.Rows.Count > 0 Then
                    row("TenBoKichDien") = BangBKD.Rows(0).Item("TenBoKichDien").ToString()
                    row("CongSuatBoKichDien") = BangBKD.Rows(0).Item("CongSuatTai")
                    row("DienApBoKichDien") = BangBKD.Rows(0).Item("DienApTai")
                    row("GiaBoKichDien") = BangBKD.Rows(0).Item("Gia")
                Else
                    row("TenBoKichDien") = "0"
                    row("CongSuatBoKichDien") = 0
                    row("DienApBoKichDien") = 0
                    row("GiaBoKichDien") = 0
                End If
                dtp.Rows.Add(row)
            End If


        Next


        'If BangPin.Rows.Count <= 1 Then
        '    TuVanDAO.ChonNhieuHeThong(TongCongSuatPin)
        'Else
        '    For Each
        '        TuVanDAO.ChonMachDieuKhien(SoTamPin, Isc)
        'End If
        Return dtp
    End Function
  
End Class
